﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ActivateYourGlutes.Infrastructure;
using ActivateYourGlutes.Infrastructure.Logging;
using ActivateYourGlutes.Services;
using MarkdownSharp;

namespace ActivateYourGlutes.Controllers
{
    public partial class AdministerResistanceExercisesController : LocalisingController
    {
        private readonly IResistanceExerciseService _service;

        public AdministerResistanceExercisesController(
            IAccountProvider accountProvider,
            ILogger logger,
            IResistanceExerciseService service) : base(accountProvider, logger)
        {
            _service = service;
        }
       
        [Authorize(Roles = "Administrator")]
        public virtual ActionResult Index()
        {
            ViewData.Model = _service.GetAll(CurrentAccount);
            return View();
        }

        [Authorize(Roles = "Administrator")]
        public virtual ActionResult Edit()
        {
            return View();
        }

        [Authorize(Roles = "Administrator")]
        public virtual ActionResult Delete(int? id)
        {
            return Json(false);
        }

        [Authorize(Roles = "Administrator")]
        public virtual ActionResult Create()
        {
            ViewData["Categories"] = GetCategorySelectItems();
            return View();
        }

        [ValidateInput(false)]
        public String ParseMarkdown(string toBeParsed)
        {
            var md = new Markdown();
            return md.Transform(toBeParsed);
        }

        private List<SelectListItem> GetCategorySelectItems()
        {
            return new List<SelectListItem>() { new SelectListItem() { Selected = false, Text = "Category", Value="C" } };
        }

        [Authorize(Roles = "Administrator")]
        [AcceptVerbs(HttpVerbs.Post)]
        public virtual ActionResult GridData(string sidx, string sord, int page, int rows)
        {
            var pageIndex = Convert.ToInt32(page) - 1;
            var pageSize = rows;
            var totalRecords = _service.TotalExercises(CurrentAccount);
            var totalPages = (int)Math.Ceiling(totalRecords / (float)pageSize);
            var exercises = _service.Exercises(CurrentAccount, pageIndex * pageSize, pageSize);

            var jsonData = new
            {
                total = totalPages,
                page,
                records = totalRecords,
                rows = (
                    from exercise in exercises.ToArray()
                    select new
                    {
                        id = exercise.ResistanceExerciseId,
                        cell = new[] {
                            "",
                            exercise.Name,
                            exercise.Category.Name,
                            exercise.Owner == null ? "System" : exercise.Owner.Username,
                            BooleanAsString(!String.IsNullOrWhiteSpace(exercise.ImageUrl)),
                            BooleanAsString(!String.IsNullOrWhiteSpace(exercise.VideoUrl)),
                            BooleanAsString(!String.IsNullOrWhiteSpace(exercise.Instructions))
                        }
                    }
                ).ToArray()
            };

            var result = Json(jsonData);
            return result;
        }

        private static string BooleanAsString(bool boolean)
        {
            return boolean ? "Yes" : "No";
        }

    }
}
